package ModuleBuilder.UI.tree;

import java.util.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.EtchedBorder;
import javax.swing.BorderFactory; 
import javax.swing.border.Border;


import java.awt.*;
import java.awt.event.*;
import java.io.*;
import ModuleBuilder.*;
import ModuleBuilder.model.*;	
import ModuleBuilder.UI.*;
import java.beans.*;

/**
 * 
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 * 
 * All Separate Tray frame are handled here
 */
public class TerrainTray extends JFrame implements ActionListener {
	public static final String _FIGURE_SIDEVIEW="side".intern();
	public static final String _FIGURE_TOPVIEW="top".intern();
	static final int _MAX_TRAYHEIGHT=500;
	
	static HashMap lookup = new HashMap();

	BuilderTrayModel model;
	JList list;
	DefaultListModel listMod;
	Dimension size = new Dimension (ImagePreview._previewIconSize+40,ImagePreview._previewIconSize+40);
	Vector currSubTray;
	TerrainTray thisTray;
	String id;	// used as a key to look up the frame so it can be made visible
	// iImagePreviewChangeListener subscribers;	// he who's interested when an image is picked
	//ArrayList subscribers;
	iImagePreviewChangeListener subscriber;
	ImageElement selectedImageElement=null;

  public TerrainTray(String name, String idIn, iImagePreviewChangeListener subIn) {
	super(name);
	createCore(name, idIn);
	setImagePreviewChangeListener(subIn);
  }
  
  public TerrainTray(String name, String idIn) {
	super(name);
	createCore(name, idIn);
  }
  
  public void setImagePreviewChangeListener(iImagePreviewChangeListener subIn) {
		subscriber=subIn;
  }
  
  private void createCore(String name, String idIn) {
	id = idIn;
	//subscribers = new ArrayList();
	Container cont = this.getContentPane();
	cont.setLayout(new BoxLayout(cont, BoxLayout.Y_AXIS));

	list = new JList();
	list.setCellRenderer(new ComboBoxRenderer() );
	list.setBorder(BorderFactory.createLoweredBevelBorder());
	cont.add(Box.createRigidArea(new Dimension(0,5)));
	JScrollPane scrollMap = new JScrollPane(list);
	cont.add(scrollMap);
	JButton okButton = new JButton("OK");
	okButton.addActionListener(this);
	cont.add(Box.createRigidArea(new Dimension(0,5)));
	cont.add(okButton);
	thisTray = this;
	lookup.put(idIn, this);  	
  }
  
  class ComboBoxRenderer extends JLabel implements ListCellRenderer {
	  public ComboBoxRenderer() {
		  setOpaque(true);
		  setHorizontalAlignment(LEFT);
		  setVerticalAlignment(CENTER);
		  setVerticalTextPosition(SwingConstants.BOTTOM);
		  setHorizontalTextPosition(SwingConstants.CENTER);
	  }
	  public Component getListCellRendererComponent(
		  JList list,
		  Object value,
		  int index,
		  boolean isSelected,
		  boolean cellHasFocus)
	  {
		 Color bc=null;
		  if (isSelected) {
			bc = list.getSelectionBackground();
			  setBackground(bc);
			  setForeground(list.getSelectionForeground());
				  
		  } else {
			bc = list.getBackground();
			  setBackground(bc);
			  setForeground(list.getForeground());
		  }
			ImageElement uv= (ImageElement)value;
		  ImageIcon icon = uv.getSmallIcon();
		  // !!! SHOULD check if u can first get the old border, then reset it to save mem 
		  Border b =  BorderFactory.createMatteBorder(5, 5, 5, 5, bc);
		  this.setBorder(b);
		  setIcon(icon);
		  return this;
	  }
  }
  class ListSelectionHandler implements ListSelectionListener {
	public void valueChanged(ListSelectionEvent e) {
		if (e.getValueIsAdjusting() == false) {
		  //String name = thisTray.getName();
		  int index = list.getSelectedIndex();
		  if ( index > -1 ) {
			// Debug.out("Terrain index=" + index + " was selected");
			// currTv = (TerrainTrayView)currSubTray.elementAt(index);
			selectedImageElement = model.getImageAt(index);
			// stick the selected el in the diction ary
			//Utils.lookup.put(Utils._TERRAIN_SELECTED_FROM_TRAY, el);
			// Util.lookup.put(Util._CURR_SELECTED_TRAY, thisTray);
			// MouseHandler mh = (MouseHandler)Util.lookup.get(Util._CURR_MOUSE_HANDLER);
			// mh.setElement( el );
			
			// inform the subscriber that there's a new image
		  }		  
		}
	}
  }

  public Dimension getPreferredSize() {
	return ( size );
  }

  public Dimension getMinimumSize() {
	return ( size );
  }

  public void setModel ( BuilderTrayModel m ) {
	model = m;
	ArrayList modelList = model.getList();
	Object[] terrainElementArray = modelList.toArray();
	list.setListData(terrainElementArray);
	size = list.getPreferredSize();
	if (size.height > _MAX_TRAYHEIGHT){
		size.height = _MAX_TRAYHEIGHT;
	}
	list.addListSelectionListener(new ListSelectionHandler());
  }

  /**
   * The current selected view is removed and the SubTray is updated
   * (THIS SHOULD NOT BE USED SINCE TERRAIN DO NOT GO AWAY WHEN PLACED)
   */
  public void removeSelectElement() {
  	Debug.out(this.toString() + ": removeSelectElement() not yet implemented!");
  }
  
  public void actionPerformed(ActionEvent e) {
  	// the "ok" button has been pushed, the item has been selected.
  	if ( selectedImageElement != null ) {
//  		for ( int i=0; i< subscribers.size(); i++) {
//			iImagePreviewChangeListener subscriber = (iImagePreviewChangeListener)subscribers.get(i);
//			subscriber.imageChanged(selectedImageElement);
//  		}
		subscriber.imageChanged(selectedImageElement);
		hide();

  	}
  }
  
  public static TerrainTray getTray( String nameIn ) {
  	return ( (TerrainTray)lookup.get(nameIn));
  }
  


}